package com.hxkcloud.patient.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/2/15.
 */

public class CustomWareView extends View {
    private static final float DEFAULT_AMPLITUDE_RATIO = 0.2f;
    private static final float DEFAULT_WATER_LEVEL_RATIO = 0.7f;
    private static final float DEFAULT_WAVE_LENGTH_RATIO = 1f;// 0.25f;
    private static final float DEFAULT_WAVE_SHIFT_RATIO = 0.0f;

    private float mWaveShiftRatio = DEFAULT_WAVE_SHIFT_RATIO;
    private float mDefaultAmplitude;
    private float mDefaultWaterLevel;
    private float mDefaultWaveLength;
    private double mDefaultAngularFrequency;
    private Paint mPaint;
    private Path mPath;
    public CustomWareView(Context context) {
        super(context);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomWareView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setStrokeWidth(2);
        mPaint.setAntiAlias(true);
        //Color.parseColor("#82d1fe"
        // mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    public CustomWareView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public CustomWareView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPaint.getShader() == null) {
            int width =getWidth();
            LinearGradient lg=new LinearGradient(0,0,width,0
                    ,new int[]{Color.parseColor("#664F9DEA"),Color.parseColor("#66867Afd")},
                    null, Shader.TileMode.MIRROR);
            mPaint.setShader(lg);
       }
        mPath.reset();
        mHeight = getHeight();
        mWidth = getWidth();
        //角频率
        mDefaultAngularFrequency = 2.0f * Math.PI / DEFAULT_WAVE_LENGTH_RATIO / getWidth();
        //振幅比换算成振幅
        mDefaultAmplitude = (mHeight ) * DEFAULT_AMPLITUDE_RATIO;
        //水位
        mDefaultWaterLevel = mHeight * DEFAULT_WATER_LEVEL_RATIO;
        //一个周期的长度
        mDefaultWaveLength = mHeight * DEFAULT_WAVE_LENGTH_RATIO;
        //第一个曲线
        final int endX = mWidth + 1;
        for (int beginX = 0; beginX < endX; beginX++) {
            double wx = ((beginX+mWaveShiftRatio*mWidth) % mWidth) * mDefaultAngularFrequency;
            float beginY = (float) (mDefaultWaterLevel + mDefaultAmplitude * Math.sin(wx));
            mPath.lineTo(beginX,beginY);
            //canvas_back.drawLine(beginX, beginY, beginX, endY, wavePaint);
        }
        mPath.lineTo(getWidth(),0);
        mPath.lineTo(0,0);
       /* mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());*/
        mPath.close();
        canvas.drawPath(mPath,mPaint);
        mPath.reset();
        //第二个曲线
        for (int beginX = 0; beginX < endX; beginX++) {
            double wx = ((beginX+mWaveShiftRatio*mWidth) % mWidth) * mDefaultAngularFrequency;
            float beginY = (float) (mDefaultWaterLevel + mDefaultAmplitude * Math.sin(wx+Math.PI*(0.5)));
            mPath.lineTo(beginX,beginY);
            //canvas_back.drawLine(beginX, beginY, beginX, endY, wavePaint);
        }
        mPath.lineTo(getWidth(),0);
        mPath.lineTo(0,0);
       /* mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());*/
        mPath.close();
        canvas.drawPath(mPath,mPaint);
    }
    private int mHeight = 0;
    private int mWidth = 0;

    public float getWaveShiftRatio() {
        return mWaveShiftRatio;
    }

    /**
     * Shift the wave horizontally according to <code>waveShiftRatio</code>.
     *
     * @param waveShiftRatio Should be 0 ~ 1. Default to be 0.
     *                       Result of waveShiftRatio multiples width of WaveView is the length to shift.
     */
    public void setWaveShiftRatio(float waveShiftRatio) {
        if (mWaveShiftRatio != waveShiftRatio) {
            mWaveShiftRatio = waveShiftRatio;
            invalidate();
        }
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (finshInflateListener!=null){
            finshInflateListener.onFinish();
        }
    }
   private FinishInflateListener finshInflateListener;

    public void setFinshInflateListener(FinishInflateListener finshInflateListener) {
        this.finshInflateListener = finshInflateListener;
    }

    public interface  FinishInflateListener{
      void   onFinish();
    }
}
